package com.jic.market.common.util;

import com.google.common.collect.Lists;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
/**
 * description: IOUtils
 * date: 2020/2/29 17:44
 * author: wangys
 * version: 1.0
 */
public class IOUtils {
    public static final String LINE_SEPARATOR_UNIX = "\n";
    public static final String LINE_SEPARATOR_WINDOWS = "\r\n";
    private static final int BUF_SIZE = 1024;

    public IOUtils() {
    }

    public static byte[] readInputStream(InputStream inStream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        boolean len = false;

        int len1;
        while((len1 = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len1);
        }

        inStream.close();
        return outStream.toByteArray();
    }

    public static List<String> readlines(Reader input) throws IOException {
        BufferedReader reader = toBufferedReader(input);
        ArrayList list = new ArrayList();

        for(String line = readline(reader); StringUtils.isNotEmpty(line); line = readline(reader)) {
            list.add(line);
        }

        return list;
    }

    public static List<String> readLines(InputStream is, int maxContinuousEmptyLinesAllowed, String charset) throws IOException {
        ArrayList lines = Lists.newArrayList();
        int emptyLineCnt = 0;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
        String line = readline(reader);

        for(boolean empty = StringUtils.isEmpty(line); !empty || emptyLineCnt < maxContinuousEmptyLinesAllowed; empty = StringUtils.isEmpty(line)) {
            if(empty) {
                ++emptyLineCnt;
            } else {
                emptyLineCnt = 0;
                lines.add(line);
            }

            line = readline(reader);
        }

        return lines;
    }

    public static BufferedReader toBufferedReader(Reader reader) {
        return reader instanceof BufferedReader?(BufferedReader)reader:new BufferedReader(reader);
    }

    public static String readline(BufferedReader reader) throws IOException {
        StringBuffer sb = new StringBuffer();
        boolean c = false;

        int c1;
        while((c1 = reader.read()) != 10 && c1 != -1) {
            if(c1 != 13) {
                sb.append((char)c1);
            }
        }

        return sb.toString();
    }

    public static void releaseStream(Object... streams) {
        Object[] var1 = streams;
        int var2 = streams.length;

        for(int var3 = 0; var3 < var2; ++var3) {
            Object stream = var1[var3];
            if(stream != null) {
                if(stream instanceof InputStream) {
                    try {
                        ((InputStream)stream).close();
                    } catch (Exception var6) {
                        var6.printStackTrace();
                    }
                } else if(stream instanceof OutputStream) {
                    try {
                        ((OutputStream)stream).close();
                    } catch (Exception var7) {
                        var7.printStackTrace();
                    }
                }

                stream = null;
            }
        }

    }

    public static String readFileFromSrc(String filename, Charset charset) throws IOException {
        InputStream is = null;

        String var3;
        try {
            is = IOUtils.class.getClassLoader().getResourceAsStream(filename);
            var3 = readFromInputStream(is, charset, false);
        } finally {
            releaseStream(new Object[]{is});
        }

        return var3;
    }

    public static String readFromInputStream(InputStream is, Charset charset, boolean withLineSeparator) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
        String line = "";

        while((line = reader.readLine()) != null) {
            sb.append(line);
            if(withLineSeparator) {
                sb.append("\n");
            }
        }

        reader.close();
        return sb.toString();
    }

    public static FileInputStream openInputStream(File file) throws IOException {
        if(file.exists()) {
            if(file.isDirectory()) {
                throw new IOException("File \'" + file + "\' exists but is a directory");
            } else if(!file.canRead()) {
                throw new IOException("File \'" + file + "\' cannot be read");
            } else {
                return new FileInputStream(file);
            }
        } else {
            throw new FileNotFoundException("File \'" + file + "\' does not exist");
        }
    }

    public static void readLineByLine(InputStream is, IOUtils.ReadConfig config, IOUtils.LineCall call) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, config.getCharsetName()), config.getBufferSize());
        String line = readline(reader);

        for(int emptyLineCnt = 0; emptyLineCnt <= config.getMaxContinuousEmptyLinesAllowed(); line = readline(reader)) {
            emptyLineCnt = StringUtils.isEmpty(line)?emptyLineCnt + 1:0;
            call.handle(line);
        }

    }

    public static void readFileLineByLine(File file, IOUtils.ReadConfig config, IOUtils.LineCall call) throws IOException {
        FileInputStream is = openInputStream(file);
        Throwable var4 = null;

        try {
            readLineByLine(is, config, call);
        } catch (Throwable var13) {
            var4 = var13;
            throw var13;
        } finally {
            if(is != null) {
                if(var4 != null) {
                    try {
                        is.close();
                    } catch (Throwable var12) {
                        var4.addSuppressed(var12);
                    }
                } else {
                    is.close();
                }
            }

        }

    }

    public interface LineCall {
        void handle(String var1);
    }

    public static class ReadConfig {
        private int bufferSize = 2097152;
        private String charsetName = "UTF-8";
        private int maxContinuousEmptyLinesAllowed = 5;

        public static IOUtils.ReadConfig getDefault() {
            return new IOUtils.ReadConfig();
        }

        public ReadConfig() {
        }

        public int getBufferSize() {
            return this.bufferSize;
        }

        public String getCharsetName() {
            return this.charsetName;
        }

        public int getMaxContinuousEmptyLinesAllowed() {
            return this.maxContinuousEmptyLinesAllowed;
        }

        public IOUtils.ReadConfig setBufferSize(int bufferSize) {
            this.bufferSize = bufferSize;
            return this;
        }

        public IOUtils.ReadConfig setCharsetName(String charsetName) {
            this.charsetName = charsetName;
            return this;
        }

        public IOUtils.ReadConfig setMaxContinuousEmptyLinesAllowed(int maxContinuousEmptyLinesAllowed) {
            this.maxContinuousEmptyLinesAllowed = maxContinuousEmptyLinesAllowed;
            return this;
        }

        @Override
        public boolean equals(Object o) {
            if(o == this) {
                return true;
            } else if(!(o instanceof IOUtils.ReadConfig)) {
                return false;
            } else {
                IOUtils.ReadConfig other = (IOUtils.ReadConfig)o;
                if(!other.canEqual(this)) {
                    return false;
                } else if(this.getBufferSize() != other.getBufferSize()) {
                    return false;
                } else {
                    String this$charsetName = this.getCharsetName();
                    String other$charsetName = other.getCharsetName();
                    if(this$charsetName == null) {
                        if(other$charsetName == null) {
                            return this.getMaxContinuousEmptyLinesAllowed() == other.getMaxContinuousEmptyLinesAllowed();
                        }
                    } else if(this$charsetName.equals(other$charsetName)) {
                        return this.getMaxContinuousEmptyLinesAllowed() == other.getMaxContinuousEmptyLinesAllowed();
                    }

                    return false;
                }
            }
        }

        protected boolean canEqual(Object other) {
            return other instanceof IOUtils.ReadConfig;
        }
        @Override
        public int hashCode() {
            boolean PRIME = true;
            byte result = 1;
            int result1 = result * 59 + this.getBufferSize();
            String $charsetName = this.getCharsetName();
            result1 = result1 * 59 + ($charsetName == null?43:$charsetName.hashCode());
            result1 = result1 * 59 + this.getMaxContinuousEmptyLinesAllowed();
            return result1;
        }
        @Override
        public String toString() {
            return "IOUtils.ReadConfig(bufferSize=" + this.getBufferSize() + ", charsetName=" + this.getCharsetName() + ", maxContinuousEmptyLinesAllowed=" + this.getMaxContinuousEmptyLinesAllowed() + ")";
        }
    }
}